# $Id$
# Copyright (c) 2012 Prabhu Gurumurthy <pgurumur@gmail.com>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

# __version__  = "$Revision$"
# __author__   = "$Author$"
# __modified__ = "$Date$"
#
PROG 			:= nvi
CC 			:= gcc
UNAME			:= $(shell uname)
PERL			:= perl
INCLUDES  	:= include
PERLLIB		:= $(shell perl -MConfig -e 'print $$Config{archlibexp}')
STRIP			:= strip
ifeq ($(UNAME), Darwin)
CFLAGS 		:= -02 -universal -arch i386 -arch x86_64 -pipe -fPIC
LDFLAGS 		:= -L/usr/lib -ltcl
EXTUTLS		:= /System/Library/Perl/5.12/ExtUtils
BUILD_OPTS	:= -prebind -headerpad_max_install_names -Wl,-search_paths_first
BUILD_OPTS 	+= -pipe -Os -Wl,-single_module -arch i386 -arch x86_64
endif

ifeq ($(UNAME), OpenBSD)
CFLAGS		:= -O2 -fPIC -I/usr/local/include/tcl8.5
# CFLAGS		+= -DHAVE_PERL_5_003_01 -DHAVE_PERL_INTERP
LDFLAGS		:= -L/usr/local/lib -ltcl85
BUILD_OPTS	:= -Wl,-R/usr/local/lib
endif

ifeq ($(UNAME), NetBSD)
CFLAGS	:= -fPIC -I/usr/pkg/include/tcl -I/usr/pkg/include
LDFLAGS	:= -L/usr/pkg/lib -ltcl85
BUILD_OPTS := -Wl,-R/usr/pkg/lib
endif

CFLAGS		+=  -Wmissing-prototypes -I$(INCLUDES) 
LDFLAGS		+= -lcurses 

CFLAGS		+= -I$(PERLLIB)/CORE
SRC_DIR   	:= src
BUILD_DIR 	:= build
BIND_DIR 	:= bindings
BINDINGS		:= build/tcl.c build/api.c
HOME			:= $(shell echo $$HOME)

PSRC			:= ${BIND_DIR}/perl.c
PSRCXS		:= ${BIND_DIR}/perl.xs
PSCRIPT		:= bindings/VI.pm
SRCS      	:= $(foreach sdir,$(SRC_DIR),$(wildcard $(sdir)/*.c))
# SRCS		:= ${PSRC}
OBJS      	:= $(patsubst src/%.c,build/%.o,$(SRCS))
OBJS			+= $(patsubst %.c,%.o, ${BINDINGS})
# OBJS		:= build/perl.o
RM 			:= /bin/rm -rf

.PHONY: all checkdirs clean perl
all: checkdirs ${PROG}
checkdirs: $(BUILD_DIR)
perl: $(PSRC)

$(PSRC):
	echo "#define _PATH_PERLSCRIPTS \"$(HOME)/apps/share/vi/perl\"" >> $@
	$(PERL) $(EXTUTLS)/xsubpp -typemap $(EXTUTLS)/typemap $(PSRCXS) >> $@
	($(PERL) -ne 'print "sub $$1 {\$$curscr->$$1(\@_)}\n" \
			if /newXS\("VI::([^":]*)"/;' $@ ; echo "1;") >> bindings/VI.pm

$(PROG) : $(OBJS) 
ifeq ($(UNAME), Linux)
		$(shell perl 'print "Cannot compile in Linux yet!\n"')
		$(shell exit 2)
else
	@echo "building $@"
	@${CC} ${BUILD_OPTS} -o $@ $^ ${LDFLAGS}
	@$(STRIP) $@
endif

vpath %.c $(SRC_DIR) $(BIND_DIR)

define make-goal
$1/%.o : %.c
ifeq ($(UNAME), Linux)
		$(shell perl -e "print 'Cannot compile in Linux yet'")
		$(shell exit)
else
	@echo "compiling $$<"
	@${CC} ${CFLAGS} -c -o $$@ $$<
endif	
endef

$(BUILD_DIR):
	@mkdir -p $@

clean:
	@echo "cleaning.... ${PROG} ${BUILD_DIR}"
	@${RM} $(PROG) $(BUILD_DIR) $(PSCRIPT) $(PSRC)

$(foreach bdir,$(BUILD_DIR),$(eval $(call make-goal,$(bdir))))
